﻿using LoadClient.YUAN.E;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadClient.YUAN.B
{
    public class ProductManager
    {

        public static List<Product> GetProductList()
        {
            return FreeSqlDB.fsql00.Queryable<Product>().ToList();
        }

        public static List<Product> GetProductListOK()
        {
            return FreeSqlDB.fsql00.Select<Product>().Where(p => p.UseFlag == true).ToList();
        }

        public static List<Product> GetProductListVisable()
        {
            return FreeSqlDB.fsql00.Select<Product>().
                Where(p => p.View == true && p.UseFlag == true).
                ToList();
        }

        public static Product GetProductByPID(int pid)
        {
            Product pp = FreeSqlDB.fsql00.Select<Product>().Where(p => p.Pid == pid).ToOne();
            return pp == null ? new Product() : pp;
        }

        public static string GetProductControlStringByPID(int pid)
        {
            return FreeSqlDB.fsql00.Select<Product>().Where(a => a.Pid.Equals(pid)).ToOne().ControlString;
        }

        public static int DeleteProductByPID(int pid)
        {
            return FreeSqlDB.fsql00.Update<Product>().Set(a => a.UseFlag, false).Set(a => a.Version, 0).Set(a => a.Name, "0").Set(a => a.Encoding, "0").Where(a => a.Pid == pid).ExecuteAffrows();
        }

        public static int GetProductReqID()
        {
            Product pt = FreeSqlDB.fsql00.Select<Product>().Where(a => a.UseFlag == false).ToOne();
            return pt == null ? 0 : pt.Pid;
        }


        public static int VisableProductByID(Product pp)
        {
            Console.WriteLine(pp.View);
            return FreeSqlDB.fsql00.Update<Product>().Set(a => a.View, pp.View).Where(a => a.Pid == pp.Pid).ExecuteAffrows();
        }

        public static int CreateEditProductByID(Product pp)
        {
            return FreeSqlDB.fsql00.Update<Product>().Set(a => a.InsertTime, DateTime.Now)
                                                     .Set(a => a.Name, pp.Name).Set(a => a.Ppid, pp.Ppid)
                                                     .Set(a => a.Encoding, pp.Encoding).Set(a => a.UseFlag, true)
                                                     .Set(a => a.ControlString, pp.ControlString)
                                                     .Set(a => a.Version + 1)
                                                     .Where(a => a.Pid == pp.Pid).ExecuteAffrows();
        }



        public static int ExistCheck(Product pp)
        {
            return FreeSqlDB.fsql00.Select<Product>().
                Where(a => a.Name == pp.Name || a.Encoding == pp.Encoding).
                ToList().Count;
        }

        public static int ExistCheckExceptID(Product pp)
        {
            return FreeSqlDB.fsql00.Select<Product>().
                Where(a => (a.Name == pp.Name || a.Encoding == pp.Encoding) && a.Pid != pp.Pid).
                ToList().Count;
        }

        public static int GetProgressID(ProductControl ptc)
        {
            ProductControl pctr = FreeSqlDB.fsql00.Select<ProductControl>().Where(a => a.Epolishing == ptc.Epolishing && a.Deoxidation == ptc.Deoxidation && a.ED1st == ptc.ED1st && a.ED2nd == ptc.ED2nd && a.Passivation == ptc.Passivation).ToOne();
            return pctr == null ? 0 : pctr.ProgressID;
        }

        public static List<ProductControlString> GetProductControlStringList()
        {
            return FreeSqlDB.fsql00.Queryable<ProductControlString>().ToList();
        }

        public static List<ProductVersion> GetProductVersionListByPID(int pid)
        {
            return FreeSqlDB.fsql00.Select<ProductVersion>().Where(a => a.ProductId.Equals(pid)).ToList();
        }

        public static int InsertProductVersionD(ProductVersion version)
        {
            return FreeSqlDB.fsql00.Insert(version).ExecuteAffrows();
        }

        public static int DeleteProductVersionByPID(int pid)
        {
            return FreeSqlDB.fsql00.Delete<ProductVersion>().Where(a => a.ProductId.Equals(pid)).ExecuteAffrows();
        }

        public static int DeleteProductVersionByVersionID(int versionid)
        {
            return FreeSqlDB.fsql00.Delete<ProductVersion>().Where(a => a.Id.Equals(versionid)).ExecuteAffrows();
        }

        public static ProductVersion GetProductVersionByVerID(int versionid)
        {
            return FreeSqlDB.fsql00.Select<ProductVersion>().Where(a => a.Id.Equals(versionid)).ToOne();
        }

        public static ProductVersion GetProductVersionString(int versionid, int pid)
        {
            return FreeSqlDB.fsql00.Select<ProductVersion>().Where(a => a.ProductId.Equals(pid) && a.VersionID.Equals(versionid)).ToOne();
        }

    }

}
